using BasicData.API.Application.Command;
using BasicData.API.Dtos;
using BasicData.ErrorCode;
using MediatR;
using System;
using System.Threading.Tasks;

namespace BasicData.API.Application.Mediator
{
    /// <summary>
    /// 用户中介者实现
    /// </summary>
    public class UserMediator : IUserMediator
    {
        private readonly IMediator _mediator;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator">MediatR中介者</param>
        public UserMediator(IMediator mediator)
        {
            _mediator = mediator;
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="command">登录命令</param>
        /// <returns>登录结果</returns>
        public async Task<APIResult<UserDTO>> LoginAsync(LoginQueryCommand command)
        {
            try
            {
                return await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                return new APIResult<UserDTO>
                {
                    Code = ResultCode.登录失败,
                    Message = $"登录失败: {ex.Message}"
                };
            }
        }

        /// <summary>
        /// 用户注册
        /// </summary>
        /// <param name="command">注册命令</param>
        /// <returns>注册结果</returns>
        public async Task<APIResult<UserDTO>> RegisterAsync(RegisterUserCommand command)
        {
            try
            {
                return await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                return new APIResult<UserDTO>
                {
                    Code = ResultCode.添加失败,
                    Message = $"注册失败: {ex.Message}"
                };
            }
        }
    }
} 